package com.wk.util;


import java.util.Map;

/**
 * 保存用户相关的变量，供后续线程内方法使用
 * 
 * @author chenxiaozhong
 */
public class RequestContext {

	public static final String USER_ID = "user_id";
	public static final String USER_CLIENT_FLAG = "user_client_flag";
	public static final String USER_NAME = "user_name";
	public static final String USER_TYPE = "user_type";
	public static final String USER_DEP_ID = "user_dep_id";
	public static final String USER_DEP_NAME = "user_dep_id";
	public static final String USER_CURRENT_COMPANY_CODE = "user_current_company_code";
	public static final String USER_APP_ID = "user_app_id";
	

	private static final ThreadLocal<Map<String, Object>> tl = new ThreadLocal<Map<String, Object>>();

	public static Map<String, Object> getAttributes() {
		Map<String, Object> attributes = tl.get();
		return attributes;
	}

	public static void setAttributes(Map<String, Object> attributes) {
		tl.set(attributes);
	}

	public static String getUserId() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_ID)) {
			return (String) attributes.get(USER_ID);
		}
		return "";
	}

	public static String getUserName() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_NAME)) {
			return (String) attributes.get(USER_NAME);
		}
		return "";
	}
	
	public static String getUserType() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_TYPE)) {
			return (String) attributes.get(USER_TYPE);
		}
		return "";
	}
	
	public static String getUserDepId() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_DEP_ID)) {
			return (String) attributes.get(USER_DEP_ID);
		}
		return "";
	}
	
	public static String getUserDepName() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_DEP_NAME)) {
			return (String) attributes.get(USER_DEP_NAME);
		}
		return "";
	}
	
	public static String getCurrentCompanyCode(){
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_CURRENT_COMPANY_CODE)) {
			return (String) attributes.get(USER_CURRENT_COMPANY_CODE);
		}
		return "";
	}

	/**
	 * @return
	 */
	public static Integer getUserClientFlag() {
		Map<String, Object> attributes = RequestContext.getAttributes();
		if (attributes != null && attributes.containsKey(USER_CLIENT_FLAG)) {
			return (Integer) attributes.get(USER_CLIENT_FLAG);
		}
		return null;
	}
}
